/**
 * user edit
 */
let systemType="";
layui.use(['form', 'tableSelect'], function () {
    var form = layui.form,
        layer = layui.layer,
        tableSelect = layui.tableSelect,
        $ = layui.jquery;
    let shipId="";
    let u=localStorage.getItem("ADMIN_LOGIN");
    let loginU={};
    if(u==undefined){layer.msg('请登录', {icon: 1, time: 500},function () {window.location = 'page/login.html';});}else{loginU=decryptLocalStorage(u);}
    let viewData=undefined;
    loadData();
    function loadData()
    {
        request.list("movieServer",{token:loginU.token},function(result){
            if(result.code==0){
                let data=result.data;
                viewData=data[0];
                console.log(JSON.stringify(data));
                if(data.length>0){
                    $("#shipId").val(viewData.shipId);
                    $("#shipName").val(viewData.shipName);
                    $("#shoreUrl").val(viewData.shoreUrl);
                    let shoreUrl=viewData.shoreUrl;
                    loadShip(shoreUrl);
                    $("#serverUrl").val(viewData.serverUrl);
                    $("#remark").val(viewData.remark);
                    $("#sonicWallName").val(viewData.sonicWallName);
                    $("#sonicWallIp").val(viewData.sonicWallIp);
                    $("#sonicWallPort").val(viewData.sonicWallPort);
                    $("#sonicWallUserName").val(viewData.sonicWallUserName);
                    $("#sonicWallPassword").val(viewData.sonicWallPassword);
                }
            }
        })
    }
    //监听提交
    form.on('submit(saveBtn)', function (data) {
        let params=data.field;
        params.token=loginU.token;
        //添加
        if(viewData==undefined){
            request.add('movieServer',JSON.stringify(params),function(result){
                if(result.code==0){
                    layer.msg('保存成功', {
                        icon: 1,
                        time: 500
                    },function () {
                        // 关闭弹出层
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                    });
                    loadData();
                }else{
                    layer.msg(result.msg || '保存失败', {
                        icon: 2,
                        time: 2000
                    });
                }
            });
        }
        //修改
        else{
            params.serverId=viewData.serverId;
            request.update('movieServer',JSON.stringify(params),function(result){
                if(result.code==0){
                    layer.msg('保存成功', {
                        icon: 1,
                        time: 500
                    },function () {
                        // 关闭弹出层
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                    });
                    loadData();
                }else{
                    layer.msg(result.msg || '保存失败', {
                        icon: 2,
                        time: 2000
                    });
                }
            });
        }
        return false;
    });

    function loadShip(shoreUrl) {
        tableSelect.render({
            elem: '#shipName',
            checkedKey: 'id',
            searchFlag: true,
            searchKey: 'shipName',
            table: {
                url: shoreUrl+"/movie/v1/shipList",
                cols: [[
                    {type: 'radio'},
                    {field: 'shipId', title: 'ID'},
                    {field: 'shipName', title: '船舶'}
                ]]
            },
            done: function (elem, data) {
                if (data.data.length > 0) {
                    shipId = data.data[0].shipId;
                    $("#shipId").val(data.data[0].shipId);
                    $("#shipName").val(data.data[0].shipName);
                } else {
                    shipId = "";
                    $("#shipId").val("");
                    $("#shipName").val("");
                }

            },
            clear: function () {
                shipId = "";
                $("#shipId").val("");
                $("#shipName").val("");

            }
        })
    }


});